/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.prozezuak;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BidaiariaSartuPane extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7771952374790469280L;
	private JPanel panel1;
	private JLabel username;
	private JLabel password;
	private JTextField textField;
	private JTextField textField2;

	private JPanel panel2;
	private JButton btn1;
	private JButton btn2;

	public BidaiariaSartuPane(final IstripuaSortu aitarenPanela) {
		super();

		this.setLayout(new BorderLayout());
		this.setTitle("Bidaiaria eta matrikula");
		this.setModal(true);
		this.setSize(300, 190);
		this.setLocationRelativeTo(aitarenPanela);

		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(4, 1));
		username = new JLabel("Bidaiariaren NAN-a");
		password = new JLabel("Matrikula");
		textField = new JTextField(12);
		textField.addKeyListener(new KeyAdapter() {// limitea NAN

					public void keyTyped(KeyEvent arg0) {
						if (textField.getText().length() >= 10) {
							arg0.consume();
						}
					}
				});
		textField2 = new JTextField(12);
		textField2.addKeyListener(new KeyAdapter() {// limitea NAN

					public void keyTyped(KeyEvent arg0) {
						if (textField2.getText().length() >= 10) {
							arg0.consume();
						}
					}
				});
		panel1.add(username);
		panel1.add(textField);
		panel1.add(password);
		panel1.add(textField2);

		btn1 = new JButton("Gehitu");
		btn2 = new JButton("Ezeztatu");
		panel2 = new JPanel(new FlowLayout());
		panel2.add(btn1);
		panel2.add(btn2);

		this.add(panel1, BorderLayout.CENTER);
		this.add(panel2, BorderLayout.SOUTH);

		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (zerbaitIdatziDu()) {
					aitarenPanela.addNewRow(textField.getText(),
							textField2.getText());
					setVisible(false);
					dispose();
				}

			}
		});

		btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
			}
		});

		this.setVisible(true);
	}

	public boolean zerbaitIdatziDu() {
		boolean aurki = false;

		aurki = (textField.getText().length() > 0 && textField2.getText()
				.length() > 0);
		return aurki;
	}
}
